<script>
const tabBarLinks = [
  "pages/index/index",
  "pages/college/college",
  "pages/create/create",
  "pages/material/material",
]; // 站点配置文件
import siteinfo from "./siteinfo.js";

// #ifdef H5
import { detectMobile } from "@/utils/user_agent.js";
// #endif

export default {
  globalData: {
    systemInfo: null, //系统信息
    api_root: siteinfo.siteroot,
    merchantId: "",
    model: "",
  },
  onLaunch: function (option) {
    this.globalData.systemInfo = uni.getSystemInfoSync();
    // #ifdef H5
    // const deviceId = option.query.did || "1D92FBE4850000";
    const deviceId = option.query.did;
    this.globalData.model = detectMobile();
    this.$store.commit("SET_DEVICE_ID", deviceId);
    uni.redirectTo({
      url: "/pages/H5/index",
    });
    // #endif

    // #ifdef MP-WEIXIN
    this.updateManager();
    const token = this.$store.state.user.token;
    // if (!token) {
    //   this.navigationTo("page_user/login/login");
    // }
    // #endif
  },
  onShow: function () {},
  onHide: function () {},
  methods: {
    //小程序主动更新
    updateManager() {
      if (!uni.canIUse("getUpdateManager")) {
        return false;
      }
      const updateManager = uni.getUpdateManager();
      updateManager.onCheckForUpdate(function (res) {
        // 请求完新版本信息的回调
        console.log("onCheckForUpdate", res);
      });
      updateManager.onUpdateReady(function (e) {
        console.log("onUpdateReady", e);
        updateManager.applyUpdate();
      });
      updateManager.onUpdateFailed(function () {
        // 新的版本下载失败
        uni.showModal({
          title: "更新提示",
          content: "新版本下载失败",
          showCancel: false,
        });
      });
    },

    // 监听globalData
    watch(method, istr) {
      var obj = this.globalData;
      Object.defineProperty(obj, istr, {
        configurable: true,
        enumerable: true,
        set: function (value) {
          method(value);
        },
        get: function (value) {
          return value;
        },
      });
    },
    /**
     * 获取tabBar页面路径列表
     */
    getTabBarLinks() {
      return tabBarLinks;
    },

    // 跳转到指定页面 (支持tabBar页面、支持h5页面)
    navigationTo(url) {
      if (!url || url.length == 0) {
        return false;
      }

      let tabBarLinks = this.getTabBarLinks(); // tabBar页面
      if (tabBarLinks.indexOf(url) > -1) {
        uni.switchTab({
          url: "/" + url,
        });
      } else {
        var position = url.indexOf("http");
        if (0 === position) {
          // #ifndef H5
          uni.navigateTo({
            url: "/pages/H5/index?src=" + encodeURIComponent(url),
          });
          // #endif
        } else {
          // 小程序页面
          uni.navigateTo({
            url: "/" + url,
          });
        }
      }
    },
    navigateBack(params, callback) {
      const pages = getCurrentPages();
      if (pages.length === 1) {
        if (typeof params === "number") {
          history.go(-params);
        } else {
          history.back();
        }
      } else {
        uni.navigateBack();
      }
    },
    /**
     * 将本地资源上传到开发者服务器
     * url
     * filePath 要上传文件资源的路径
     * name 文件对应的 key , 开发者在服务器端通过这个 key 可以获取到文件二进制内容
     * formData HTTP 请求中其他额外的 form data
     * complete 请求完成
     */
    _uploadFile(url, filePath, name, formData, success, fail, complete) {
      formData = formData || {};
      let header = {};

      if (this.globalData.token) {
        header = {
          token: "Bearer " + this.globalData.token,
        };
      }
      uni.uploadFile({
        url: this.globalData.api_root + url,
        filePath: filePath,
        name: name,
        formData: formData,
        header: header || {},
        complete: (res) => {
          let result =
            typeof res.data === "object" ? res.data : JSON.parse(res.data);
          if (0 == result.code || 200 == result.code) {
            success && success(result.data);
          } else {
            fail && fail(result.msg);
          }
          complete && complete();
        },
      });
      // uploadTask.onProgressUpdate((res) => {
      // 	console.log('上传' + res);
      // 	console.log('上传进度' + res.progress);
      // 	console.log('已经上传的数据长度' + res.totalBytesSent);
      // 	console.log('预期需要上传的数据总长度' + res.totalBytesExpectedToSend);

      // 	// 测试条件，取消上传任务。
      // 	if (res.progress > 50000) {
      // 		uploadTask.abort();
      // 	}
      // });
    },
    // 上传图片
    _uploadImage(filePath, formData, success, fail, complete) {
      this._uploadFile(
        "/system/oss/upload",
        filePath,
        "file",
        formData,
        success,
        fail,
        complete
      );
    },
  },
};
</script>

<style>
/*每个页面公共css */
@import "@/static/iconfont-dy.css";
</style>
